const express = require("express")
const service = require("../service/adminService")
const jwt = require("../utils/jwt")
const { formatResponse } = require("../utils/tool")
const { ValidationError } = require("../utils/error")

const router = express.Router()

router.put("/", async function(req, res, next) {
    const result = await service.updateAdminService(res, req.body)
    res.send(formatResponse(0, "修改成功", result))
})

router.post("/login", async function(req, res, next) {
    // 验证码的验证
    if (req.body.captcha.toLowerCase() !== req.session.captcha.toLowerCase()) {
        res.send(new ValidationError("验证码错误").toResponseJSONError())
    }

    const result = await service.loginService(res, req.body)

    res.send(formatResponse(0, "", result))
})

router.get("/whoami", async function(req, res, next) {

    res.send(formatResponse(0, "", req.info))
})

module.exports = router